package vip.xiaonuo.auth.api.context;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.auth.api.SaBaseLoginUserApi;
import vip.xiaonuo.auth.core.pojo.SaBaseClientLoginUser;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.feign.ClientLoginUserFeign;

import java.util.List;


@Slf4j
@RequiredArgsConstructor
@Component("clientLoginUserApi")
public class ClientLoginUserApiContextBean implements SaBaseLoginUserApi {

    private final ClientLoginUserFeign clientLoginUserFeign;

    /**
     * 根据id获取B端用户信息，查不到则返回null
     *
     * @param id
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseLoginUser getUserById(String id) {
        return this.clientLoginUserFeign.getUserById(id);
    }

    /**
     * 根据id获取C端用户信息，查不到则返回null
     *
     * @param id
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseClientLoginUser getClientUserById(String id) {
        return this.clientLoginUserFeign.getClientUserById(id);
    }

    /**
     * 根据账号获取B端用户信息，查不到则返回null
     *
     * @param account
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseLoginUser getUserByAccount(String account) {
        return this.clientLoginUserFeign.getUserByAccount(account);
    }

    /**
     * 根据账号获取C端用户信息，查不到则返回null
     *
     * @param account
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseClientLoginUser getClientUserByAccount(String account) {
        return this.clientLoginUserFeign.getClientUserByPhone(account);
    }

    /**
     * 根据手机号获取B端用户信息，查不到则返回null
     *
     * @param phone
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseLoginUser getUserByPhone(String phone) {
        return this.clientLoginUserFeign.getUserByPhone(phone);
    }

    /**
     * 根据邮箱获取B端用户信息，查不到则返回null
     *
     * @param email
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseLoginUser getUserByEmail(String email) {
        return this.clientLoginUserFeign.getUserByEmail(email);
    }

    /**
     * 根据手机号获取C端用户信息，查不到则返回null
     *
     * @param phone
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseClientLoginUser getClientUserByPhone(String phone) {
        return this.clientLoginUserFeign.getClientUserByAccount(phone);
    }

    /**
     * 根据邮箱获取C端用户信息，查不到则返回null
     *
     * @param email
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseClientLoginUser getClientUserByEmail(String email) {
        return this.clientLoginUserFeign.getClientUserByEmail(email);
    }

    /**
     * 根据用户id获取用户集合
     *
     * @param userIdList
     * @author xuyuxiang
     * @date 2022/4/27 22:53
     */
    @Override
    public List<JSONObject> listUserByUserIdList(List<String> userIdList) {
        String feignResp = this.clientLoginUserFeign.listUserByUserIdList(userIdList);
        List<JSONObject> resp = null;
        if(StrUtil.isNotBlank(feignResp)){
            resp = (List<JSONObject>) JSONUtil.toBean(feignResp,List.class);
        }
        return resp;
    }

    /**
     * 根据用户id获取角色集合
     *
     * @param userId
     * @author xuyuxiang
     * @date 2022/4/27 22:53
     */
    @Override
    public List<JSONObject> getRoleListByUserId(String userId) {
        String feignResp = this.clientLoginUserFeign.getRoleListByUserId(userId);
        List<JSONObject> resp = null;
        if(StrUtil.isNotBlank(feignResp)){
            resp = (List<JSONObject>) JSONUtil.toBean(feignResp,List.class);
        }
        return resp;
    }

    /**
     * 根据角色id和用户id集合获取按钮码集合
     *
     * @param userAndRoleIdList
     * @author xuyuxiang
     * @date 2022/4/27 22:54
     */
    @Override
    public List<String> getButtonCodeListListByUserAndRoleIdList(List<String> userAndRoleIdList) {
        return this.clientLoginUserFeign.getButtonCodeListListByUserAndRoleIdList(userAndRoleIdList);
    }

    /**
     * 根据角色id和用户id集合获取移动端按钮码集合
     *
     * @param userAndRoleIdList
     * @author xuyuxiang
     * @date 2022/4/27 22:54
     */
    @Override
    public List<String> getMobileButtonCodeListListByUserIdAndRoleIdList(List<String> userAndRoleIdList) {
        return this.clientLoginUserFeign.getMobileButtonCodeListListByUserIdAndRoleIdList(userAndRoleIdList);
    }

    /**
     * 根据角色id和用户id集合获取权限集合
     *
     * @param userAndRoleIdList
     * @param orgId
     * @author xuyuxiang
     * @date 2022/4/27 22:54
     */
    @Override
    public List<JSONObject> getPermissionListByUserIdAndRoleIdList(List<String> userAndRoleIdList, String orgId) {
        String feignResp = this.clientLoginUserFeign.getPermissionListByUserIdAndRoleIdList(userAndRoleIdList, orgId);
        List<JSONObject> resp = null;
        if(StrUtil.isNotBlank(feignResp)){
            resp = (List<JSONObject>) JSONUtil.toBean(feignResp,List.class);
        }
        return resp;
    }

    /**
     * 更新用户的登录时间和登录ip等信息
     *
     * @param userId
     * @param device
     * @author xuyuxiang
     * @date 2022/4/27 22:57
     */
    @Override
    public void updateUserLoginInfo(String userId, String device) {
        this.clientLoginUserFeign.updateUserLoginInfo(userId, device);
    }

    /**
     * 使用手机号创建B端用户
     *
     * @param phone
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseLoginUser createUserWithPhone(String phone) {
        return this.clientLoginUserFeign.createUserWithPhone(phone);
    }

    /**
     * 使用手机号创建C端用户
     *
     * @param phone
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseClientLoginUser createClientUserWithPhone(String phone) {
        return this.clientLoginUserFeign.createClientUserWithPhone(phone);
    }

    /**
     * 使用邮箱创建B端用户
     *
     * @param email
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseLoginUser createUserWithEmail(String email) {
        return this.clientLoginUserFeign.createUserWithEmail(email);
    }

    /**
     * 使用邮箱创建C端用户
     *
     * @param email
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public SaBaseClientLoginUser createClientUserWithEmail(String email) {
        return this.clientLoginUserFeign.createClientUserWithEmail(email);
    }

    /**
     * 执行注册
     *
     * @param account
     * @param password
     * @author xuyuxiang
     * @date 2022/3/10 16:14
     */
    @Override
    public void doRegister(String account, String password) {
        this.clientLoginUserFeign.doRegister(account, password);
    }
}
